<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\Descuentos;
use Rebecca\RebeccaBundle\Form\DescuentosType;

/**
 * Descuentos controller.
 *
 */
class DescuentosController extends Controller
{
    /**
     * Lists all Descuentos entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
        $component = $this->get("components.Descuentos");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:Descuentos:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a Descuentos entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
        $component = $this->get("components.Descuentos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Descuentos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Descuentos:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
    /**
     * Displays a form to select a new Employee entity.
     *
     */
    public function selectAction()
    {
        $component = $this->get("components.Contratos");

        $entities = $component->getAll();

        if (!$entities) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        
        $path = $this->generateUrl("descuentos_new");
        
        return $this->render('RebeccaBundle:Contratos:select.html.twig', array(
                    'entities' => $entities,  
                    'action'  => $path,
                    'title' => 'Descuentos'
                ));
               
    }

    /**
     * Displays a form to create a new Descuentos entity.
     *
     */
    public function newAction()
    {
        $id = $this->getRequest()->get('idContrato');
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");
        
        /* @var $periodosComponent \Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $periodosComponent = $this->get("components.PeriodosLaborales");
        
        /* @var $contrato \Rebecca\RebeccaBundle\Entity\Contratos */
        $contrato = $component->get($id);
        
        $periodos = $periodosComponent->getTipoNomina($contrato->getIdTipoNomina()->getId()); 
        
        $entity = new Descuentos();
        $form   = $this->createForm(new DescuentosType(), $entity);

        return $this->render('RebeccaBundle:Descuentos:new.html.twig', array(
            'entity' => $entity,
            'contrato' => $contrato,
            'periodos' => $periodos,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new Descuentos entity.
     *
     */
    public function createAction()
    {
        /* @var $componentPeriodoLaboralContrato Rebecca\RebeccaBundle\Component\PeriodosLaboralesContratosComponent */
        $componentPeriodoLaboralContrato = $this->get("components.PeriodosLaboralesContratos");
        
        $entity  = new Descuentos();
        $request = $this->getRequest();
        
        $idPeriodoLaboral = $request->get("idPeriodoLaboral");
        $idContrato = $request->get("idContrato");
        
        $periodoLaboralContrato = $componentPeriodoLaboralContrato->getUnique($idPeriodoLaboral, $idContrato);
        
        $entity->setIdPeriodoLaboralContrato($periodoLaboralContrato);
        $entity->setFechaCreacion(date_create());
        
        $form    = $this->createForm(new DescuentosType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
            $component = $this->get("components.Descuentos");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('descuentos'));
            
        }

        return $this->render('RebeccaBundle:Descuentos:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing Descuentos entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
        $component = $this->get("components.Descuentos");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Descuentos entity.');
        }

        $editForm = $this->createForm(new DescuentosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Descuentos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Descuentos entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
        $component = $this->get("components.Descuentos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Descuentos entity.');
        }

        $editForm   = $this->createForm(new DescuentosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('descuentos'));
        }

        return $this->render('RebeccaBundle:Descuentos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Descuentos entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\DescuentosComponent */
            $component = $this->get("components.Descuentos");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Descuentos entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('descuentos'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
